package top.lbqaq.servlet;

import top.lbqaq.domain.PageBean;
import top.lbqaq.error.NullObjectException;
import top.lbqaq.pojo.Material;
import top.lbqaq.service.MaterialService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author yzj
 * @Date 2021/11/22
 */

public class MaterialServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methods = req.getParameter("methodd");
        if ("select".equals(methods)) {
            String currentPage = req.getParameter("currentPage");
            if (currentPage == null) {
                currentPage = String.valueOf(1);
            }
            String rows = String.valueOf(10);
            String method = req.getParameter("method");
            String condition = req.getParameter("mingcheng");
            if ("selcetByCategory".equals(method)) {
                this.selectByCategory(req, resp, method, condition, currentPage, rows);
            } else if ("selectByName".equals(method)) {
                this.selectByName(req, resp, method, condition, currentPage, rows);
            } else if ("selectAll".equals(method)) {
                this.selectAll(req, resp, method, currentPage, rows);
            }
        } else if ("Inselect".equals(methods)) {
            this.Inselect(req, resp);
        } else if ("insert".equals(methods)) {
            this.insert(req, resp);
        } else if ("update".equals(methods)) {
            this.update(req, resp);
        } else if ("goupdate".equals(methods)) {
            this.goupdate(req, resp);
        } else if ("delete".equals(methods)) {
            this.delete(req, resp);
        } else if ("materialin".equals(methods)) {
            this.materialIn(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void selectByCategory(HttpServletRequest req, HttpServletResponse resp, String method, String condition, String currentPage, String rows) throws ServletException, IOException {
        MaterialService materialService = new MaterialService();
        PageBean<Material> pb = materialService.selectByCategory(condition, currentPage, rows);
        pb.setWay(method);
        pb.setName(condition);
        req.setAttribute("materialList", pb);
        req.getRequestDispatcher("/jsp/mdeleteandupdate.jsp").forward(req, resp);
    }

    private void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int i = 0;
        Material material = new Material();
        MaterialService materialService = new MaterialService();
        String name = req.getParameter("name");
        String date = req.getParameter("date");
        if (name != null) {
            List<Material> materials = materialService.selectInByName(name);
            for (int j = 0; j < materials.size(); j++) {
                if (materials.get(j).getMaterialInDate().equals(date)) {
                    material = set(material, materials.get(j).getMaterialCategory(), materials.get(j).getMaterialName(),
                            materials.get(j).getMaterialUnit(), materials.get(j).getMaterialUnitPrice(),
                            materials.get(j).getMaterialInAmount(), materials.get(j).getMaterialInRemark(),materials.get(j).getMaterialCategory());
                    break;
                }
            }
        } else {
            material = getAndSet(material, req, 0);
        }
        if (material.getFlag() == 0) {
            if ((i = materialService.insertMaterial(material)) == 1) {
                req.setAttribute("message", "插入成功");
                req.getRequestDispatcher("/jsp/minsert.jsp").forward(req, resp);
            } else if (i == 0) {
                req.setAttribute("message", "插入失败");
                req.getRequestDispatcher("/jsp/minsert.jsp").forward(req, resp);
            } else if (i == -1) {
                req.setAttribute("message", "请勿重复插入");
                req.getRequestDispatcher("/jsp/minsert.jsp").forward(req, resp);
            }
        } else if (material.getFlag() == 1) {
            if ((i = materialService.insertMaterial(material)) == 1) {
                req.setAttribute("message", "入库成功");
                req.getRequestDispatcher("/material?methodd=Inselect&mingcheng=" + name).forward(req, resp);
            } else if (i == 0) {
                req.setAttribute("message", "入库失败");
                req.getRequestDispatcher("/material?methodd=Inselect&mingcheng=" + name).forward(req, resp);
            }
        }

    }

    private void selectByName(HttpServletRequest req, HttpServletResponse resp, String method, String condition, String currentPage, String rows) throws ServletException, IOException {
        MaterialService materialService = new MaterialService();
        PageBean<Material> pb = materialService.selectByName(condition, currentPage, rows);
        pb.setWay(method);
        pb.setName(condition);
        req.setAttribute("materialList", pb);
        req.getRequestDispatcher("/jsp/mdeleteandupdate.jsp").forward(req, resp);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String way = req.getParameter("mthodd");
        String condition = req.getParameter("method");
        MaterialService materialService = new MaterialService();
        int i = materialService.deleteByName(name);
        if (i == 1) {
            req.setAttribute("message", "删除成功");
            req.getRequestDispatcher("/material?methodd=select&method=" + way + "&mingcheng=" + condition).forward(req, resp);
        } else if (i == 0) {
            req.setAttribute("message", "删除失败");
            req.getRequestDispatcher("/material?methodd=select&method=" + way + "&mingcheng=" + condition).forward(req, resp);
        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp, String method, String currentPage, String rows) throws ServletException, IOException {
        MaterialService materialService = new MaterialService();
        PageBean<Material> pb = materialService.selectAllMaterial(currentPage, rows);
        pb.setWay(method);
        req.setAttribute("materialList", pb);
        req.getRequestDispatcher("/jsp/mdeleteandupdate.jsp").forward(req, resp);
    }

    private void goupdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String way = req.getParameter("mthodd");
        String condition = req.getParameter("method");
        MaterialService materialService = new MaterialService();
        Material materials = materialService.selectByName1(name);
        req.setAttribute("condition", condition);
        req.setAttribute("way", way);
        req.setAttribute("material", materials);
        req.getRequestDispatcher("/jsp/mupdate.jsp").forward(req, resp);
    }

    private void Inselect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");//当前页码
        if (currentPage == null) {
            currentPage = String.valueOf(1);
        }
        String rows = String.valueOf(10);
        MaterialService materialService = new MaterialService();
        String condition = req.getParameter("mingcheng");
        PageBean<Material> pb = materialService.selectInByName1(condition, currentPage, rows);
        pb.setName(condition);
        System.out.println(pb.getName());
        req.setAttribute("materialList", pb);
        req.getRequestDispatcher("/jsp/materialacc.jsp").forward(req, resp);
    }

    private void materialIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int i = 0;
        Material material = new Material();
        material = getAndSet(material, req, 3);
        MaterialService materialService = new MaterialService();
        try {
            i = materialService.materialin(material);
            if (i == 1) {
                req.setAttribute("message", "采购成功");
                req.getRequestDispatcher("/jsp/materialin.jsp").forward(req, resp);
            } else if (i == 0) {
                req.setAttribute("message", "采购失败");
                req.getRequestDispatcher("/jsp/materialin.jsp").forward(req, resp);
            }
        } catch (NullObjectException e) {
            req.setAttribute("message", "无此供货单位记录，请先添加供货单位信息");
            req.getRequestDispatcher("/jsp/supplier/edit.jsp").forward(req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String way = req.getParameter("method");
        String condition = req.getParameter("condition");
        Material material = new Material();
        MaterialService materialService = new MaterialService();
        material = getAndSet(material, req, 1);
        int i = 0;
        if ((i = materialService.updateByName(material)) == 1) {
            req.setAttribute("way", way);
            req.setAttribute("condition", condition);
            req.setAttribute("message", "更新成功");
            req.getRequestDispatcher("/jsp/mupdate.jsp").forward(req, resp);
        } else if (i == 0) {
            req.setAttribute("way", way);
            req.setAttribute("condition", condition);
            req.setAttribute("message", "更新失败");
            req.getRequestDispatcher("/jsp/mupdate.jsp").forward(req, resp);
        }
    }

    Material set(Material material, String leibie, String mingcheng, String danwei, long danjia, long jine, String beizhu,String shiyong) {
        material.setMaterialCategory(leibie);
        material.setMaterialName(mingcheng);
        material.setMaterialUnit(danwei);
        material.setMaterialUnitPrice(danjia);
        material.setMaterialAmount(jine);
        material.setMaterialUse(shiyong);
        material.setMaterialRemark(beizhu);
        material.setFlag(1);//材料库手动录入
        return material;

    }

    Material getAndSet(Material material, HttpServletRequest req, int x) {
        String mingcheng = req.getParameter("mingcheng");
        String leibie = req.getParameter("leibie");
        String danwei = req.getParameter("danwei");
        long danjia = Long.parseLong(req.getParameter("danjia"));
        long jine = Long.parseLong(req.getParameter("jine"));
        String shiyong = req.getParameter("shiyong");
        String beizhu = req.getParameter("beizhu");
        String gongsi = req.getParameter("gongsi");
        String shuliang = req.getParameter("shuliang");
        String riqi = req.getParameter("riqi");
        material.setMaterialCategory(leibie);
        material.setMaterialName(mingcheng);
        material.setMaterialUnit(danwei);
        material.setMaterialUnitPrice(danjia);
        material.setMaterialAmount(jine);
        material.setMaterialUse(shiyong);
        material.setMaterialRemark(beizhu);
        material.setFlag(x);
        material.setMaterialInAmount(jine);
        material.setSupplyCompany(gongsi);
        material.setMaterialInRemark(beizhu);
        material.setMaterialInQuantity(shuliang);
        material.setMaterialInDate(riqi);
        return material;
    }


}


